/*
 * @Description: vue路由配置
 * @Author: ligy
 * @Date: 2022-03-20 21:49:42
 * @LastEditTime: 2022-03-21 00:21:27
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import {getSessionItem} from '../utils/sessionStorage'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to代表将要访问的路径
  // from代表从哪个路径跳转而来
  //next 是一个函数,表示放行
  if(to.path === '/login'){
    return next()
  } else {
    const token = getSessionItem('token')
    if(token){
      return next()
    } else {
      return next('/login')
    }
  }
})
export default router
